package com.green.card.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.green.card.R;
import com.green.card.base.BaseFragment;
import com.green.card.constants.Config;
import com.green.card.smscode.GenruiSmsCode;
import com.green.card.utils.MobileNOUtils;
import com.green.card.utils.ToastUtils;
import com.zyasdk.util.SP;


/**
 * Created by admin on 2018/1/25.
 * @des 注册登陆
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {
    /**
     * 使用SP保存到本地的唯一批处理的ID
     */
    private static final String BATCHID = Config.BATCHID;

    //获取手机号码
    private EditText mEtPhone;
    // 获取短信验证码
    private EditText mEtPasee;
    // 获取验证码
    private TextView mGetWorld;
    // 登陆
    private Button mLogin;
    // 手机号码
    private String mPhoneNumber;
    //title注册
    private TextView mTitleRegister;

    // 短信验证码
    private String mPassCode;

    private int countSeconds = 60;//倒计时秒数
    private Handler mCountHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (countSeconds > 0) {
                --countSeconds;
                mGetWorld.setText("(" + countSeconds + ")后获取验证码");
                mCountHandler.sendEmptyMessageDelayed(0, 1000);
            } else {
                countSeconds = 60;
                mGetWorld.setText("请重新获取验证码");
            }
        }
    };

    @Override
    protected int setContentView() {
        int contentViewId = R.layout.fragment_mine;
        return contentViewId;
    }

    /**
     * findViewById
     * @param view
     */
    @Override
    public void findView(View view) {
        mEtPhone = (EditText) view.findViewById(R.id.mine_et_phone);
        mEtPasee = (EditText) view.findViewById(R.id.mine_et_pass);
        mGetWorld = (TextView) view.findViewById(R.id.tv_get_world);
        mLogin = (Button) view.findViewById(R.id.mine_btn_login);
        mTitleRegister = (TextView) view.findViewById(R.id.title_register);
    }

    @Override
    public void setListener() {
        mGetWorld.setOnClickListener(this);
        mLogin.setOnClickListener(this);
    }

    @Override
    public void initData(Activity context) {
        mTitleRegister.setVisibility(View.GONE);
        String batchId = SP.get(context, BATCHID);
        if (!batchId.isEmpty()){//如果不为空，说明已经进行过手机号+验证码 验证过了。
            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction().replace(R.id.fl_mine, new HomeFragment());
            transaction.commit();
            mLogin.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.mine_btn_login:
                login();
                break;
            case R.id.tv_get_world://获取验证码
                getSMSCode();
                break;
            default:
                break;
        }
    }

    /**
     * 登陆
     */
    private void login() {
        mPassCode = mEtPasee.getText().toString().trim();
        //获取发送的验证码
        int mSmsCode = GenruiSmsCode.getmSmsCode();
        String smsCode = String.valueOf(mSmsCode);
        if (mPassCode.equals(smsCode)){
            //替换为HomeFragment
            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction().replace(R.id.fl_mine, new HomeFragment());
            transaction.commit();
            //隐藏登陆按钮
            mLogin.setVisibility(View.GONE);
            mEtPasee.getText().clear();
            //下面两行代码是隐藏软键盘
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
        }else {
            ToastUtils.toast(context,"请输入正确的验证码");
            mEtPasee.getText().clear();
        }
    }

    /**
     * 获取短信验证码请求
     */
    private void getSMSCode() {
        if (countSeconds == 60){
            //获取到手机号
            mPhoneNumber = mEtPhone.getText().toString().trim();
            boolean isMobileNO = MobileNOUtils.isMobileNO(mPhoneNumber);
            if (isMobileNO){
                startCountBack();
                GenruiSmsCode.sendVerificationCode(getActivity(),mPhoneNumber);
            }else {
                ToastUtils.toast(context,"请输入正确的手机号码");
                mEtPhone.getText().clear();
            }

        }else {
            ToastUtils.toast(context,"您操作太频繁了，请稍等再试");
        }
    }



    /**
     * 获取验证码信息,进行计时操作
     */
    private void startCountBack() {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mGetWorld.setText(countSeconds + "");
                mCountHandler.sendEmptyMessage(0);
            }
        });
    }


}
